#include<bits/stdc++.h>
#define ll long long
#define MAXN 200005
using namespace std;
int x[MAXN],y[MAXN],w[MAXN];
ll T,cnt,a[MAXN],p[MAXN],f[MAXN];
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%lld",&T);
    while(T--)
    {
        memset(f,0,sizeof(f));
        memset(p,0,sizeof(p));
        cnt=0;
        ll n;
        scanf("%lld",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%lld",&a[i]);
            if(p[a[i]])
            {
                ++cnt;
                x[cnt]=i;
                y[cnt]=p[a[i]];
                w[cnt]=a[i];
            }
            p[a[i]]=i;
        }
        for(int i=1;i<=n-1;++i)
            for(int j=1;j<=n-i;++j)
                if(x[j]>x[j]+1)
                {
                    swap(x[j],x[j+1]);
                    swap(y[j],y[j+1]);
                    swap(w[j],w[j+1]);
                }
        for(int i=1;i<=cnt;++i)
        {
            f[i]=w[i];
            for(int j=1;j<i;++j)
            {
                if(y[j]<y[i])
                    f[i]=max(f[i],f[j]+w[i]);
            }

        }
        ll ans=0;
        for(int i=1;i<=cnt;++i)
            if(f[i]>ans)ans=f[i];
        printf("%lld\n",ans);
    }
    return 0;
}
